package org.chapter.service;

import org.chapter.Util.PropsUtil;
import org.chapter.helper.DatabaseHelper;
import org.chapter.model.Customer;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Mr.no.
 * User:yangyun
 * Date:2017/9/24
 * Time:18:11
 */
public class CustomerService {
    private static final String DRIVER;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);

    static {
        Properties conf = null;
        try {
            conf = PropsUtil.loadProps("config.properties");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        DRIVER = conf.getProperty("jdbc.driver");
        URL = conf.getProperty("jdbc.url");
        USERNAME = conf.getProperty("jdbc.username");
        PASSWORD = conf.getProperty("jdbc.password");

        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            LOGGER.error("can not load jdbc driver", e);
        }

    }


    //获取客户列表
    public List<Customer> getCustomerList(){
            String sql = "SELECT * FROM customer";
            return DatabaseHelper.queryEntityList(Customer.class,sql);
    }
    //获取客户
    public Customer getCustomer(long id){

        return null;
    }
    //创建客户
    public boolean createCustomer(Map<String,Object> fieldMap){
        return DatabaseHelper.insertEntity(Customer.class,fieldMap);
    }
    //更新客户
    public boolean updateCustomer(long id,Map<String,Object> fieldMap){
        return DatabaseHelper.updateEntity(Customer.class,id,fieldMap);
    }
    //删除客户
    public boolean deleteCustomer(long id){
        return DatabaseHelper.deleteEntity(Customer.class,id);
    }
}
